using System;
using System.ComponentModel;
using System.Windows.Input;
using System.Threading.Tasks;
using ErpManager.WPF.Services;

namespace ErpManager.WPF.ViewModels
{
    public class RegisterViewModel : INotifyPropertyChanged
    {
        private readonly AuthenticationService _authService;
        private string _username = string.Empty;
        private string _password = string.Empty;
        private string _confirmPassword = string.Empty;
        private string _errorMessage = string.Empty;

        public event PropertyChangedEventHandler? PropertyChanged;

        public string Username
        {
            get => _username;
            set
            {
                _username = value;
                OnPropertyChanged(nameof(Username));
                ValidateInput();
            }
        }

        public string Password
        {
            get => _password;
            set
            {
                _password = value;
                OnPropertyChanged(nameof(Password));
                ValidateInput();
            }
        }

        public string ConfirmPassword
        {
            get => _confirmPassword;
            set
            {
                _confirmPassword = value;
                OnPropertyChanged(nameof(ConfirmPassword));
                ValidateInput();
            }
        }

        public string ErrorMessage
        {
            get => _errorMessage;
            set
            {
                _errorMessage = value;
                OnPropertyChanged(nameof(ErrorMessage));
            }
        }

        public ICommand RegisterCommand { get; }
        public ICommand CancelCommand { get; }

        public event Action? RegistrationSuccessful;

        public RegisterViewModel(AuthenticationService authService)
        {
            _authService = authService;
            RegisterCommand = new RelayCommand(async () => await Register(), CanRegister);
            CancelCommand = new RelayCommand(Cancel);
        }

        private async Task Register()
        {
            if (!ValidateInput())
            {
                return;
            }

            var (success, message) = await _authService.RegisterAsync(Username, Password);
            if (success)
            {
                RegistrationSuccessful?.Invoke();
            }
            else
            {
                ErrorMessage = message;
            }
        }

        private void Cancel()
        {
            // 关闭窗口的逻辑将在View中实现
        }

        private bool CanRegister()
        {
            return !string.IsNullOrWhiteSpace(Username) &&
                   !string.IsNullOrWhiteSpace(Password) &&
                   !string.IsNullOrWhiteSpace(ConfirmPassword) &&
                   Password == ConfirmPassword &&
                   Password.Length >= 6;
        }

        private bool ValidateInput()
        {
            if (string.IsNullOrWhiteSpace(Username))
            {
                ErrorMessage = "请输入用户名";
                return false;
            }

            if (string.IsNullOrWhiteSpace(Password))
            {
                ErrorMessage = "请输入密码";
                return false;
            }

            if (Password.Length < 6)
            {
                ErrorMessage = "密码长度至少为6位";
                return false;
            }

            if (Password != ConfirmPassword)
            {
                ErrorMessage = "两次输入的密码不一致";
                return false;
            }

            ErrorMessage = string.Empty;
            return true;
        }

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
